<?php
// {{{ includes
load_class('FLEA_Db_TableDataGateway');
load_class('FLEA_WebControls');
// }}

/**
 * 多数Model的基类, 提供一些公共方法
 */
 class Model_Base extends FLEA_Db_TableDataGateway {
	 
 	/**
 	 * 构造函数
 	 *
 	 * @return Model_Base
 	 */
 
  	function Model_Base() {
	  parent::FLEA_Db_TableDataGateway();
  	}
      
  	
	
    
    function getPageBar(& $PageData){
    	
    	  
    	foreach ($_GET as $key => $val){
    		if($key == 'page') continue;
    		else {
    			$column[]=$key.'='.$val;
    			$fileds.='<input type="hidden" name="'.$key.'" value="'.$val.'" />';
    		}
    	}
    	$str=implode('&',$column);
    	 
    	$PageBar=' '.$PageData['currentPageNumber'].' / '.$PageData['pageCount'].'';
		
    	$url=$_SERVER['PHP_SELF'].'?'.$str;
    	$first=$PageData['currentPage'] == '0'?'<img src="images/icon_first.jpg" width="39" height="17" border=0>&nbsp;':'<a href="'.$url.'&page='.$PageData['firstPage'].'"><img src="images/icon_first.jpg" width="39" height="17" border=0></a>&nbsp;';
    	$prev=$PageData['currentPage'] == '0'?'<img src="images/icon_pre.jpg" width="26" height="17" border=0>&nbsp;':'<a href="'.$url.'&page='.$PageData['prevPage'].'"><img src="images/icon_pre.jpg" width="26" height="17" border=0></a>&nbsp;';
		$next=$PageData['lastPageNumber'] >= $PageData['pageCount']?'<img src="images/icon_next.jpg" width="27" height="17" border=0>&nbsp;':'<a href="'.$url.'&page='.$PageData['nextPage'].'"><img src="images/icon_next.jpg" width="27" height="17" border=0></a>&nbsp;';    	
    	$last=$PageData['lastPageNumber'] >= $PageData['pageCount']?'<img src="images/icon_last.jpg" width="39" height="17" border=0>&nbsp;&nbsp;&nbsp;&nbsp; ':'<a href="'.$url.'&page='.$PageData['lastPage'].'"><img src="images/icon_last.jpg" width="39" height="17" border=0></a>&nbsp;&nbsp;&nbsp;&nbsp; ';    	
    	
    	$PageBar='<form method="get" action="">
		    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="pagetion">
		              <tr>
		                <td width="40%" height="30" align="left">&nbsp;<img src="images/icon_data.jpg" width="10" height="10"> Data：'.$PageData['totalCount'].' 
		                    &nbsp;<input name="page" type="text" id="page" value="1" size="1" onfocus="this.value=\'\';">
		      /'.$PageData['pageCount'].' 
		      <input name="imageField2" type="image" id="imageField2" src="images/icon_go.jpg"  align="absmiddle"></td>
		                <td width="24%">&nbsp;</td>
		                <td width="36%" align="left"> 
		                '.$first.$prev.$next.$last.'	
		                <a href="#" onclick="window.location.reload();"><img src="images/icon_refresh.jpg" width="27" height="17" border=0></a>&nbsp;&nbsp;                
		                <img src="images/icon_top.jpg" width="14" height="17" border=0>&nbsp;&nbsp; 
		                '.$fileds.'
		                </td>
		      </tr>
            </table></form>
            ';
    	 
    	
    	 
    	
    	return $PageBar;
    	  
    	
    	/*
    	foreach ($_GET as $key => $val){
    		if($key == 'page' || $key == 'controller' || $key == 'action') continue;
    		else $attribs[$key]=$val;
    	}
    	//$str=implode('&',$column);
    	
    	$attribs['pager']=$PageData;
    	$attribs['controller']=$_GET['controller'];
    	$attribs['action']=$_GET['action'];
    	
    	return  $this->_ctlPagernav('pageNav',$attribs);
    	*/
    }
    
    
    function _ctlPagernav($name, $attribs)
	{
	    /**
	     * FLEA_WebControls::extractAttribs() 从 $attribs 数组中导出一个包含指定选项的数组，
	     * 导出的同时，会从 $attribs 中删除这些指定的选项。
	     *
	     * 返回的数组包含所有指定的选项。
	     * 如果指定的选项没有在 $attribs 中找到，则该选项的值为 null。
	     */
	    $opts = array('pager', 'controller', 'action', 'length', 'slider', 'prevLabel', 'nextLabel');
	    $data = FLEA_WebControls::extractAttribs($attribs, $opts);
	
	    /**
	     * FLEA_WebControls::mergeAttribs() 用于将 $attribs 中嵌套的数组合并到 $attribs 中。
	     */
	    FLEA_WebControls::mergeAttribs($attribs);
	
	    if ($data['slider'] <= 0) { $data['slider'] = 2; }
	    if ($data['length'] <= 0) { $data['length'] = 5; }
	   
	    if ($data['prevLabel'] == '') { $data['prevLabel'] = '上页'; }
	    if ($data['nextLabel'] == '') { $data['nextLabel'] = '下页'; }
	
	    $output = "<ul";
	    if ($name) {
	        $name = h($name);
	        $output .= " id=\"{$name}\"";
	    }
	    $output .= ">\n";
	    
	    $output.="<li>共".$data['pager']['totalCount']."条记录  </li>\n";
	    
	    if ($data['pager']['currentPage'] == $data['pager']['firstPage']) {
	        $output .= "<li class=\"disabled\">&#171; {$data['prevLabel']}</li>\n";
	    } else {
	        $attribs['page'] = $data['pager']['prevPage'];
	        $url = url($data['controller'], $data['action'], $attribs);
	        $output .= "<li><a href=\"{$url}\">&#171; {$data['prevLabel']}</a></li>\n";
	    }
	
	    $currentPage = $data['pager']['currentPage'];
	
	    $mid = intval($data['length'] / 2);
	    if ($currentPage < $data['pager']['firstPage']) {
	        $currentPage = $data['pager']['firstPage'];
	    }
	    if ($currentPage > $data['pager']['lastPage']) {
	        $currentPage = $data['pager']['lastPage'];
	    }
	
	    $begin = $currentPage - $mid;
	    if ($begin < $data['pager']['firstPage']) { $begin = $data['pager']['firstPage']; }
	    $end = $begin + $data['length'] - 1;
	    if ($end >= $data['pager']['lastPage']) {
	        $end = $data['pager']['lastPage'];
	        $begin = $end - $data['length'] + 1;
	        if ($begin < $data['pager']['firstPage']) { $begin = $data['pager']['firstPage']; }
	    }
	
	    if ($begin > $data['pager']['firstPage']) {
	        for ($i = $data['pager']['firstPage']; $i < $data['pager']['firstPage'] + $data['slider'] && $i < $begin; $i++) {
	            $attribs['page'] = $i;
	            $in = $i + 1;
	            $url = url($data['controller'], $data['action'], $attribs);
	            $output .= "<li><a href=\"{$url}\">{$in}</a></li>\n";
	        }
	
	        if ($i < $begin) {
	            $output .= "<li class=\"none\">...</li>\n";
	        }
	    }
	
	    for ($i = $begin; $i <= $end; $i++) {
	        $attribs['page'] = $i;
	        $in = $i + 1;
	        if ($i == $data['pager']['currentPage']) {
	            $output .= "<li class=\"current\">{$in}</li>\n";
	        } else {
	            $url = url($data['controller'], $data['action'], $attribs);
	            $output .= "<li><a href=\"{$url}\">{$in}</a></li>\n";
	        }
	    }
	
	    if ($data['pager']['lastPage'] - $end > $data['slider']) {
	        $output .= "<li class=\"none\">...</li>\n";
	        $end = $data['pager']['lastPage'] - $data['slider'];
	    }
	
	    for ($i = $end + 1; $i <= $data['pager']['lastPage']; $i++) {
	        $attribs['page'] = $i;
	        $in = $i + 1;
	        $url = url($data['controller'], $data['action'], $attribs);
	        $output .= "<li><a href=\"{$url}\">{$in}</a></li>\n";
	    }
	
	    if ($data['pager']['currentPage'] == $data['pager']['lastPage']) {
	        $output .= "<li class=\"disabled\">{$data['nextLabel']} &#187;</li>\n";
	    } else {
	        $attribs['page'] = $data['pager']['nextPage'];
	        $url = url($data['controller'], $data['action'], $attribs);
	        $output .= "<li><a href=\"{$url}\">{$data['nextLabel']} &#187;</a></li>\n";
	    }
	
	    $output .= "</ul>\n";
	
		return $output;
	}
    
    
    
    /**
     * Send Email
     *
     * @param string $from
     * @param string $to
     * @param string $subject
     * @param string $content
     * @param string $format
     * @param string $attachment
     * 
     * @return bloon
     */
    
    function SendMail($from='',$to,$subject,$content,$format='text',$attachment=''){
    	$mail=& get_singleton('Model_Email');
    	
    	$mail->setFrom($from);
    	$mail->setTo($to);
		$mail->setSubject($subject);
    	$mail->setAttachments($inAttachments);
    	if('text'==$format){
    		$mail->setText($content);
    	}
    	else{
    		$mail->setHtml($content);
    	}
    	
    	return $mail->Send();
    }
}//End Class
?>
